/*
 * Copyright (c) 2007-2020 西安交通信息投资营运有限公司 版权所有
 * 作者:焦元义
 */

import {post, tree} from '@/utils/request';
import {ApiResult} from '@/utils/data';
import {exportWeb} from '@/utils/utils';
import config from '@/config';
import {SysDicModel} from './data';
import React from "react";
import {FolderOpenOutlined} from "@ant-design/icons/lib";

/** 获取系统字典接口基地址 */
const getUrl = (url: string) => config.apiBaseUrl + '/sys/dic' + url;

/** 系统字典接口服务 */
const service = {
  // /** 是否存在指定主键的系统字典 */
  // async existById(id: string): Promise<ApiResult<boolean>> {
  //   return post(getUrl('/existById'), {id});
  // },
  //
  // /** 是否存在指定名称的系统字典 */
  // async existByName(name: string, excludeId: string): Promise<ApiResult<boolean>> {
  //   return post(getUrl('/existByName'), {name, excludeId});
  // },
  //
  // /** 是否存在指定编码的系统字典 */
  // async existByCode(code: string, excludeId: string): Promise<ApiResult<boolean>> {
  //   return post(getUrl('/existByCode'), {code, excludeId});
  // },

  /** 新建系统字典 */
  async insert(data: SysDicModel): Promise<ApiResult> {
    // debugger;
    return post(getUrl('/insert'), data);
  },

  /** 修改系统字典 */
  async update(data: SysDicModel): Promise<ApiResult> {
    // debugger;
    return post(getUrl('/update'), data);
  },

  /** 保存系统字典 */
  async save(created: boolean, oldRecord: any, newRecord: any): Promise<ApiResult> {
    return created ? service.insert(newRecord) : service.update(newRecord)
  },

  // /** 批量保存系统字典 */
  // async batchSave(data: SysDicModel[]) {
  //   return post(getUrl('/batchSave'), data);
  // },

  /** 修改系统字典状态 */
  async updateStatus(ids: string, status: boolean | number): Promise<ApiResult> {
    status = !status;
    return post(getUrl('/updateStatus'), {ids, status});
  },

  /** 根据主键字符串删除系统字典 */
  async delete(ids: string): Promise<ApiResult> {
    return post(getUrl('/delete'), {ids});
  },

  /** 根据主键查询单个系统字典 */
  async selectById(id: string): Promise<ApiResult<SysDicModel>> {
    return post(getUrl('/selectById'), {id});
  },

  // /** 根据编码查询单个系统字典 */
  // async selectByCode(code: string): Promise<ApiResult<SysDicModel>> {
  //   return post(getUrl('/selectByCode'), {code});
  // },

  /** 查询系统字典列表 */
  async selectList(data: any) {
    return tree(getUrl('/selectList'), data);
  },
  /** 根据类型编码查询字典列表 */
  async selectListByCategoryCode(categoryCode: string): Promise<ApiResult<SysDicModel[]>> {
    return post(getUrl('/selectListByCategoryCode'), {categoryCode});
  },

  /** 转换Tree节点属性 */
  convertTreeNode(model: SysDicModel) {
    model.title = model.name;
    model.value = model.id;
    model.key = model.id;
    // FolderOutlined
    model.parentId == "0" ? model.icon = (<FolderOpenOutlined/>) : model.icon = (
      <i className="icon-notebook"/>);//设置图标
  },

  /***
   * 通过编码查询树列表
   * @param categoryCode 所选编码
   */
  async selectTreeByCategoryCode(categoryCode: string) {
    return tree(getUrl('/selectListByCategoryCode'), {categoryCode}, service.convertTreeNode);
  },

  // /** 获取系统字典导出字段 */
  // async exportNames() {
  //   return post(getUrl('/exportNames'));
  // },

  /** 导出系统字典列表 */
  export(data: any) {
    exportWeb(getUrl('/exportWeb'), data);
  },
}
export default service;
